<template>
   <el-card style="max-width: 1400px">
    <el-input v-model="SupplierName" style="width: 240px" placeholder="请输入供应商名称" />
  
    <el-button type="primary" @click="getsupplier">查询</el-button>

    <el-button type="primary" @click="addsupplier">新增</el-button>

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
 
      <el-table-column type="selection" width="55" />

      <el-table-column property="supplierName" label="供应商" width="120px"/>
      
      <el-table-column property="address" label="地址" width="120px" />
  
      <el-table-column property="contactPerson" label="对接人" width="120px"/>

      <el-table-column property="contactPhone" label="对接电话" width="120px"/>
      
      <el-table-column property="email" label="邮箱" width="200px"/>

     
      <el-table-column property="businessType" label="主营" width="120px"/>
        
      <el-table-column property="registeredCapital" label="注册资产" width="120px"/>

      <el-table-column property="status" label="状态" width="120px">
          <template  #default="rows">
            <el-text class="mx-1" type="danger" v-if="rows.row.status===0">禁用</el-text>
            <el-text class="mx-1" type="success" v-else-if="rows.row.status===1">启用</el-text>
            <el-text class="mx-1" type="warning" v-else>冻结</el-text>
          </template>
      </el-table-column>
      <el-table-column>
        <template #default="scope">
          <!-- 编辑按钮 -->
          <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
        <!-- 删除按钮 -->
          <el-button type="danger"  @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页模块，用于切换页面和设置每页显示数量 -->
    <el-pagination
      v-model:current-page="page"
      v-model:page-size="size"
      :page-sizes="[8, 10, 12, 14]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>

  <el-dialog  v-model="Isdisable" :title="disabled ? '编辑供应商' : '新增供应商'" width="30%" align-center>
    <el-form :model="Supplier" :rules="rules" ref="formRef" label-width="auto">
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="Supplier.supplierName" placeholder="请输入供应商名称"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="Supplier.address" placeholder="请输入地址"></el-input>
      </el-form-item>
      <el-form-item label="对接人" prop="contactPerson">
        <el-input v-model="Supplier.contactPerson" placeholder="请输入对接人"></el-input>
      </el-form-item>
      <el-form-item label="对接电话" prop="contactPhone">
        <el-input v-model="Supplier.contactPhone" placeholder="请输入对接电话"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="Supplier.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="主营" prop="businessType">
        <el-input v-model="Supplier.businessType" placeholder="请输入主营"></el-input>
      </el-form-item>
      <el-form-item label="注册资产" prop="registeredCapital">
        <el-input v-model="Supplier.registeredCapital" placeholder="请输入注册资产"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="Supplier.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="2">冻结</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template  #footer>
      <el-button @click="Isdisable = false">取 消</el-button>
      <el-button type="primary" @click="submitForm"  :loading ="submitting">
        {{ submitting ? '提交中...' :disabled? '保存修改' : '立即创建' }}
      </el-button>

    </template>
  </el-dialog>

  </template>

  <script lang="js" setup>
  // 引入Vue的响应式和生命周期钩子
  import { ref, onMounted } from 'vue'
  // 引入获取库存数据的接口
  import { getlist,addlist,deletelist,updatelist } from '@/api/supplier'
import { ElMessage, ElMessageBox } from 'element-plus'

  // 定义输入框绑定的响应式变量
  const SupplierName = ref('')
  // 定义表格数据的响应式变量
  const tableData = ref([])
  // 定义当前页码的响应式变量
  const page = ref(1)
  // 定义每页显示数量的响应式变量
  const size = ref(8)
  // 定义数据总数的响应式变量
  const total = ref(0)

  const disabled = ref(false)

  const Isdisable = ref(false)
  
  const submitting = ref(false)

  const formRef = ref(null)

  const Supplier = ref(
    {
      supplierName: '',
      address: '',
      contactPerson: '',
      contactPhone: '',
      email: '',
      businessType: '',
      registeredCapital: '',
      status: '' 
    }
  )
  // 组件挂载后调用getList方法获取数据
  onMounted(() => {
    getsupplier()
  })

  const getsupplier = async() => {
    // 调用接口获取数据
    const res = await getlist({
        SupplierName: SupplierName.value,
        page: page.value,
        size: size.value
     })
    // 判断接口返回状态码是否为200
    if (res.data.code === "200") {
      // 更新表格数据
      tableData.value = res.data.data.supplierPage
      // 更新数据总数
      total.value = res.data.data.total

    }
  }

  const handleSizeChange = (val) => {
    // 更新每页显示数量
    size.value = val
    // 重新获取数据
    getsupplier()
  }


  const handleCurrentChange = (val) => {
    // 更新当前页码
    page.value = val
    // 重新获取数据
    getsupplier()
  }
  
  const submitForm = async() => {
    try{
      if (disabled.value) {
        await updatelist(Supplier.value)
        ElMessage({
          type:'success',
          message: '修改成功',
        }) 
      }else{
        await addlist(Supplier.value)
        ElMessage({
          type:'success',
          message: '添加成功',
        })
      }
      Isdisable.value=false
      await getsupplier()
    }catch(error){
      ElMessage({
        type: 'error',
        message: '提交失败', 
      })
    }finally{
      submitting.value=false
    }
  }

  const addsupplier = () => {
    disabled.value=false
    resetForm()
    Isdisable.value=true
  }

  const edit = (row) => {
    Isdisable.value = true;
    Supplier.value = { ...row };   // 复制对象
    disabled.value = true;
}

  // 重置表单
  const resetForm = () => {
  Supplier.value = {
    supplierName: '',
    address: '',
    contactPerson: '', 
    contactPhone: '',
    email: '',
    businessType: '',
    registeredCapital: '',
    status: 1 // 设置默认状态
  };
}

  const del = (row) => {
    ElMessageBox.confirm(
      '确定要删除数据吗？',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
    ).then(async() => {
      // 调用删除接口
     try{
      await deletelist(row.supplierId)
      ElMessage({
        type:'success',
        message: '删除成功',
      })
     }catch(error){
      ElMessage({
        type: 'error',
        message: '删除失败', 
      })
      }
      // 删除成功后重新获取数据
      getsupplier()
      ElMessage.success('删除成功')
        }).catch(() => {
      ElMessage.info('已取消删除')
      } 
    )
  }

  // 定义验证规则
  const rules = {
    supplierName: [
      { required: true, message: '请输入供应商名称', trigger: 'blur' },
      { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
    ],
    address: [
      { required: true, message: '请输入地址', trigger: 'blur' },
      { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' } 
    ],
    contactPerson: [
      { required: true, message: '请输入对接人', trigger: 'blur' },
      { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' } 
    ],
    contactPhone: [
      { required: true, message: '请输入对接电话', trigger: 'blur' },
      { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
      { min: 11, max: 11, message: '长度应为 11 个字符', trigger: 'blur' } 
    ],
    email: [
      { required: true, message: '请输入邮箱', trigger: 'blur' },
        { 
          pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/, 
          message: '邮箱格式不正确', 
        trigger: 'blur' 
        }
      ],
    businessType: [
      { required: true, message: '请输入主营', trigger: 'blur' },
      { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
    ],
    registeredCapital: [
      { required: true, message: '请输入注册资产', trigger: 'blur' },
      { pattern: /^\d+$/, message: '注册资产必须为数字', trigger: 'blur' }
    ],
  }



  </script>

  <style scoped>
  /* 设置按钮的左外边距 */
  .el-button {
    margin-left: 10px;
  }
  </style>